﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FileMover.SharedControls
{
    public partial class VisibleLog : UserControl, FileMover.Service.ILog
    {
        private readonly BindingList<string> _messages = new BindingList<string>();
        public VisibleLog()
        {
            InitializeComponent();
            lbMessages.DataSource = _messages;
        }

        #region ILog Member
        delegate void Action<T1>(T1 t);

        private void AddMessage(string message)
        {
            _messages.Add(message);
        }

        public void Log(FileMover.Service.LevelEnum level, string format, params object[] formatParams)
        {
            var buffer = new StringBuilder();
            buffer.AppendFormat("[{0}] ", level.ToString());
            buffer.AppendFormat(format, formatParams);

            if (this.InvokeRequired)
            {
                Action<string> action = AddMessage;
                this.Invoke(action, buffer.ToString());
            }
            else
            {
                AddMessage(buffer.ToString());
            }
        }

        #endregion
    }
}
